using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_DieuChuyenDA.
    /// Date: 19/07/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_DieuChuyenDA : Object
    {
        #region NewID

        public string NewID(string key)
        {
            return SqlHelperSmart.GenCode("TBL_DieuChuyen", "Code", key, 6);
        }

        #endregion

        #region ExistBy

        public bool ExistById(
            int id)
        {
            bool Result = false;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.StoredProcedure, "TBL_DieuChuyen_Exist",
                                                                    new[] {new SqlParameter("@id", id)});
            if (myReader != null)
            {
                Result = myReader.HasRows;
                myReader.Close();
                myReader.Dispose();
            }
            return Result;
        }

        #endregion

        #region GetBy

        public TBL_DieuChuyen GetById(
            int id)
        {
            TBL_DieuChuyen obj = null;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader("TBL_DieuChuyen_Get",
                                                                    new[] {new SqlParameter("@id", id)});
            if (myReader != null)
            {
                if (myReader.Read())
                {
                    obj = Populate(myReader);
                }
                myReader.Close();
                myReader.Dispose();
            }
            return obj;
        }

        #endregion

        #region Populate

        public TBL_DieuChuyen Populate(
            IDataReader myReader)
        {
            TBL_DieuChuyen obj = null;
            if (myReader != null)
            {
                obj = new TBL_DieuChuyen();
                if (!myReader.IsDBNull(myReader.GetOrdinal("id")))
                {
                    obj.id = Convert.ToInt32(myReader["id"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaTB")))
                {
                    obj.MaTB = Convert.ToInt32(myReader["MaTB"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaBCdi")))
                {
                    obj.MaBCdi = Convert.ToInt32(myReader["MaBCdi"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaBCDen")))
                {
                    obj.MaBCDen = Convert.ToInt32(myReader["MaBCDen"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("Ngay")))
                {
                    obj.Ngay = Convert.ToDateTime(myReader["Ngay"]);
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("GhiChu")))
                {
                    obj.GhiChu = Convert.ToString(myReader["GhiChu"]);
                }
            }
            return obj;
        }

        #endregion

        #region Add

        public int Add(
            int MaTB,
            int MaBCdi,
            int MaBCDen,
            DateTime Ngay,
            string GhiChu)
        {
            return Add(new TBL_DieuChuyen
                           {
                               MaTB = MaTB,
                               MaBCdi = MaBCdi,
                               MaBCDen = MaBCDen,
                               Ngay = Ngay,
                               GhiChu = GhiChu
                           });
        }

        public int Add
            (
            TBL_DieuChuyen obj)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_Insert",
                                                    new[]
                                                        {
                                                            new SqlParameter("MaTB", obj.MaTB),
                                                            new SqlParameter("MaBCdi", obj.MaBCdi),
                                                            new SqlParameter("MaBCDen", obj.MaBCDen),
                                                            new SqlParameter("Ngay", obj.Ngay),
                                                            new SqlParameter("GhiChu", obj.GhiChu),
                                                        });
        }

        public int Duplicate(
            string id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_Duplicate",
                                                    new[] {new SqlParameter("@id", id)});
        }

        #endregion

        #region Update

        public int Update(
            int id,
            int MaTB,
            int MaBCdi,
            int MaBCDen,
            DateTime Ngay,
            string GhiChu)
        {
            return Update(new TBL_DieuChuyen
                              {
                                  id = id,
                                  MaTB = MaTB,
                                  MaBCdi = MaBCdi,
                                  MaBCDen = MaBCDen,
                                  Ngay = Ngay,
                                  GhiChu = GhiChu
                              });
        }

        public int Update
            (TBL_DieuChuyen obj)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_Update",
                                                    new[]
                                                        {
                                                            new SqlParameter("id", obj.id),
                                                            new SqlParameter("MaTB", obj.MaTB),
                                                            new SqlParameter("MaBCdi", obj.MaBCdi),
                                                            new SqlParameter("MaBCDen", obj.MaBCDen),
                                                            new SqlParameter("Ngay", obj.Ngay),
                                                            new SqlParameter("GhiChu", obj.GhiChu),
                                                        });
        }

        #endregion

        #region Delete

        public int Delete(
            int id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_Delete", new[]
                                                                                                              {
                                                                                                                  new SqlParameter
                                                                                                                      ("@id",
                                                                                                                       id)
                                                                                                                  ,
                                                                                                              });
        }

        public int Destroy(
            string id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_DestroyBy",
                                                    new[] {new SqlParameter("@id", id)});
        }

        public int Destroy()
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_DieuChuyen_Destroy");
        }

        #endregion

        #region GetDataSet 

        public DataSet GetDataSet()
        {
            return SqlHelper.Helper.ExecuteDataSet("TBL_DieuChuyen_GetList");
        }

        #endregion

        #region GetDataTable 

        public DataTable GetDataTable()
        {
            return SqlHelper.Helper.ExecuteDataTable("TBL_DieuChuyen_GetList");
        }

        #endregion

        #region GetList 

        public List<TBL_DieuChuyen> GetList()
        {
            using (
                IDataReader reader = SqlHelper.Helper.ExecuteReader(CommandType.StoredProcedure,
                                                                    "TBL_DieuChuyen_GetList"))
            {
                var list = new List<TBL_DieuChuyen>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        #endregion

        #region Count

        public int Count()
        {
            return (int) SqlHelper.Helper.ExecuteScalar(CommandType.StoredProcedure, "TBL_DieuChuyen_Count");
        }

        #endregion Count
    }
}